/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function calTime(){
    var currentETime = new Date();
    var eTime = currentETime.getTime();
    $('#responseTime').html(eTime - sTime);
}
var sTime;
function startTime(){
    var currentSTime = new Date();
    sTime = currentSTime.getTime();
    $('#responseTime').html("<img src=\"./images/loader.gif\" />");
}
function loadRoomInfoTable(){
      $.blockUI();
 $.post("LoadRoomTable",{
        action: "loadroominfotable"
    },
    function(data){

        $("#showTable").html(data);

    },
    "html"

    );
}
 

function loadRoom(){
    $.blockUI();
 $.post("LoadRoomTable",{
        action: "loadroom"
    },
    function(data){

        for( i in data )
        {

            $('#'+i).attr("src", data[i]);


        }

    },
    "json"

    );
        $.blockUI();
   $.post("LoadRoomTable",{
        action: "loadroomqty"
    },
    function(data){

        for( i in data )
        {

            $('#QTY'+i).text(data[i]);


        }

    },
    "json"

    );
  //เรียกใช้ BlockUI
}

function  loadRoomInfo(){
 $.blockUI();
    $.post("LoadRoomTable",{
        action: "loadroominfo",
        shelf:$("#shelf").val()
    },
    function(data){
        if(data.success){
           
            $("#productname").val(data.productname);
            $("#tgatype").val(data.tgatype);
            $("#bondpad").val(data.bondpad);
            $('#room img').each(function()
            {
                $(this).attr('class', '');
            });
            $("#"+data.shelfno).attr('class', 'rollover_img');

        }else{
            alert("can't load shelf data");
        }

    },
    "json"

    );
   
}
function editRoom(shelfno){
    $("#shelf").val(shelfno);
    loadRoomInfo();
}

$(document).ready(function()
{
    loadRoom();
    // Make sure to only match links to wikipedia with a rel tag
    $('#room img').each(function()
    {
        // We make use of the .each() loop to gain access to each element via the "this" keyword...
        $(this).qtip(
        {
            content: {
                text: 'Loading...', // The text to use whilst the AJAX request is loading
                ajax: {
                    url: 'LoadRoomTable', // URL to the local file
                    type: 'POST', // POST or GET
                    data: "action=loadroomtooltip&shelf="+$(this).attr('id'), // Data to pass along with your request
                    success: function(data, status) {
                        // Process the data

                        // Set the content manually (required!)
                        this.set('content.text', data);
                    }
                }
            },
            position: {
                my: 'left center',  // Position my top left...
                at: 'right center', // at the bottom right of...
                adjust: {
                    screen: true
                } // Keep the tooltip on-screen at all times
            },
            style: {
                classes: "ui-tooltip-dark"

            }

        })


    });
    $('#room img').each(function()
    {
        $(this).click(function() {
            
            $('#room img').each(function()
            {
                $(this).attr('class', '');
            });
            $(this).attr('class', 'rollover_img');
            $("#shelf").val($(this).attr('id'));
            loadRoomInfo();
            loadRoomInfoTable();
          
        });
    });


   jQuery("#inputform").validationEngine('attach', {
        onValidationComplete: function(form, status){
            if(status){
                var oksumit=false;
                var shelf1=$("#shelf").val();
                var productname1=$("#productname").val();
                var tgatype1 = $("#tgatype").val();
                var bondpad1 = $("#bondpad").val();

                            oksumit=confirm("Do you want to edit room shelf? ");
                            if(oksumit) {
                            $.blockUI();
                                $.post(
                                    "RoomSettingServlet",
                                    {
                                        action: "updateshelf",
                                        shelf: shelf1 ,
                                        productname:productname1,
                                        tgatype:tgatype1,
                                        bondpad:bondpad1
                                    },
                                    function(data){
                                        if (data.success){
                                            alert("Success to edit room shelf ");
                                            loadRoomInfoTable();
                                        }
                                        else
                                            alert("Error can't edit data ");
                                    },
                                    "json"

                                    );
                                    
                            }





            }
        }
    });


                 
});